/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package controllers

import (
	"strings"
	"time"

	"WebAdminSys/libs"
	"WebAdminSys/models"
)

type ProfileUserController struct {
	BaseController
}

func (self *ProfileUserController) ModifyUserInFo() {
	self.Data["pageTitle"] = "资料修改"
	id := self.userId
	Admin, _ := models.ManageAdminGetById(id)
	row := make(map[string]interface{})
	row["id"] = Admin.Id
	row["login_name"] = Admin.LoginName
	row["real_name"] = Admin.RealName
	row["phone"] = Admin.Phone
	row["email"] = Admin.Email
	self.Data["admin"] = row
	self.display()
}

func (self *ProfileUserController) ModifyUserPass() {
	Admin_id, _ := self.GetInt("id")
	Admin, _ := models.ManageAdminGetById(Admin_id)
	//修改
	Admin.Id = Admin_id
	Admin.UpdateTime = time.Now().Unix()
	Admin.UpdateId = self.userId
	Admin.LoginName = strings.TrimSpace(self.GetString("login_name"))
	Admin.RealName = strings.TrimSpace(self.GetString("real_name"))


	pwdOld := strings.TrimSpace(self.GetString("password_old"))
	pwdOldMd5 := libs.GetEnCodeByMd5(pwdOld + Admin.Salt)
	if Admin.Password != pwdOldMd5 {
		self.ajaxMsg("旧密码错误", MSG_ERR)
		return;
	}

	pwdNew1 := strings.TrimSpace(self.GetString("password_new1"))
	pwdNew2 := strings.TrimSpace(self.GetString("password_new2"))
	
	if pwdNew1 != pwdNew2 {
		self.ajaxMsg("两次密码不一致", MSG_ERR)
		return
	}
	if len(pwdNew1) < 6{
		self.ajaxMsg("新密码不能少于6位", MSG_ERR)
	}

	pwd, salt := libs.Password(4, pwdNew1)
	Admin.Password = pwd
	Admin.Salt = salt
	
	Admin.UpdateTime = time.Now().Unix()
	Admin.UpdateId = self.userId
	Admin.Status = 1

	if err := Admin.Update("Password","UpdateTime","Salt"); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}
